package com.imooc.order.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;

/**
 * Created by deng_wb on 2019/6/15
 */
@RestController
@DefaultProperties(defaultFallback = "defaultFallback")
public class HystrixController {

//    @HystrixCommand(fallbackMethod = "fallback")
//    HystrixCommandProperties  配置查询
//   1. 服务超时配置
    /**@HystrixCommand(commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })**/

//    2. 服务熔断配置
   /* @HystrixCommand(commandProperties = {
            @HystrixProperty(name="circuitBreaker.enabled",value = "true"), // 开启断路器
            @HystrixProperty(name="circuitBreaker.requestVolumeThreshold",value = "10"), // 最小请求量
            @HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value = "10000"), // 发生熔断时的休眠时间窗
            @HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value = "60"), // 触发断路器的错误百分比
    })*/

    @HystrixCommand
    @GetMapping("/getProductInfoList")
    public String getProductInfoList(@RequestParam("number")Integer number){
        if (number%2==0){
            return "success";
        }

        RestTemplate restTemplate = new RestTemplate();

//        throw new RuntimeException("发生了异常一样会降级");

        return restTemplate.postForObject("http://127.0.0.1:8001/product/listForOrder",
                Arrays.asList("157875196366160022"),String.class);


    }

    private String fallback(){

        return "太拥挤了,请稍后重试~~";
    }

    private String defaultFallback(){
        return "defaultFallback::: 太拥挤了,请稍后重试~~";

    }
}
